package org.yonggan.shop.utils

import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession
import org.yonggan.shop.constant.ConfigurationManager

object SparkUtils {


  /**
    * 提取SparkConf
    * @param appName 任务名称
    * @return SparkConf
    */
  def getSparkConf(appName: String): SparkConf = {
    var local = true
    try {
      local = ConfigurationManager.ISLOCAL
    } catch {
      case e: Exception =>
    }
    val master = if (local) "local[*]" else ConfigurationManager.MASTER
    new SparkConf().setAppName(appName).setMaster(master)

  }


  /**
    * 创建 SparkSession 对象
    *
    * @param appName 应用的名称
    * @return SparkSession
    */
  def getSparkSession(appName: String): SparkSession = {
    var local = true
    try {
      local = ConfigurationManager.ISLOCAL
    } catch {
      case e: Exception =>
    }
    val master = if (local) "local" else ConfigurationManager.MASTER
    SparkSession.builder().appName(appName).master(master).getOrCreate()
  }

  /**
    * 创建 SparkSession 对象
    *
    * @param conf 应用的名称
    * @return SparkSession
    */
  def getSparkSession(conf: SparkConf): SparkSession = {
    var local = true
    try {
      local = ConfigurationManager.ISLOCAL
    } catch {
      case e: Exception =>
    }
    val master = if (local) "local" else ConfigurationManager.MASTER
    SparkSession.builder().config(conf).getOrCreate()
  }

}
